<template>
  <!-- 地址添加修改页面 -->
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? '新增地址' : '编辑地址'" :close-on-click-modal="false"
             :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" label-width="120px">
      <el-form-item prop="addressMark" label="地址别名">
        <ren-select v-model="dataForm.addressMark" dict-type="addressMark" placeholder="请选择地址别名"></ren-select>
      </el-form-item>
      <el-form-item prop="isDefault" label="默认地址">
        <ren-radio-group v-model="dataForm.isDefault" dict-type="is_default" placeholder="请选择默认地址"></ren-radio-group>
      </el-form-item>
      <el-form-item prop="receiveName" label="收货人">
        <el-input v-model="dataForm.receiveName" placeholder="请输入收货人姓名"></el-input>
      </el-form-item>
      <el-form-item prop="receivePhone" label="电话号码">
        <el-input v-model="dataForm.receivePhone" placeholder="请输入收货人电话号码"></el-input>
      </el-form-item>
      <el-form-item prop="receiveProvince" label="所在省份">
        <el-input v-model="dataForm.receiveProvince" placeholder="请输入收货地址所在的省、州或自治区"></el-input>
      </el-form-item>
      <el-form-item prop="receiveCity" label="所在城市">
        <el-input v-model="dataForm.receiveCity" placeholder="请输入所在的城市"></el-input>
      </el-form-item>
      <el-form-item prop="receiveCounty" label="所在区县">
        <el-input v-model="dataForm.receiveCounty" placeholder="请输入收货地址所在的区县"></el-input>
      </el-form-item>
      <el-form-item prop="receiveAddress" label="详细地址">
        <el-input v-model="dataForm.receiveAddress" placeholder="请输入详细的街道地址，如门牌号、楼层等"></el-input>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确认</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'

export default {
  data() {
    return {
      visible: false,
      url: '',
      dialogImageUrl: '',
      dialogVisible: false,
      imagelist:{},
      dataForm: {
        id: '',
        isDefault: '',          //为每个地址项添加一个可选的别名字段，允许用户为地址设置自定义标识，如“家”、“办公室”或“朋友家”
        addressMark: '',        //该地址是否为用户的默认地址
        receiveAddress:'',      //记录详细的街道地址，如门牌号、楼层等
        receiveCity: '',        //记录收货地址所在的城市
        receiveCounty: '',      //记录收货地址所在的区县
        receiveName: '',        //记录收货人的姓名。
        receivePhone: '',       //记录收货人的手机号码
        receiveProvince: '',    //记录收货地址所在的省、州或自治区
        userId: ''              //关联到用户表的ID，用于与用户进行关联，表示该地址属于哪个用户
      }
    }
  },
  computed: {
    dataRule() {
      return {
        receiveAddress: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        receiveCity: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        receiveCounty: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        receiveName: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        receivePhone: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'},
          {pattern: /^(?:(?:\+|00)86)?1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur'}
        ],
        receiveProvince: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        userId: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    init() {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo() {
      this.$http.get(`/api/userAddress/${this.dataForm.id}`).then(({data: res}) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {
      })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/api/userAddress', this.dataForm).then(({data: res}) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, {'leading': true, 'trailing': false}),
  }
}
</script>
<style>
.el-upload el-upload-picture-card {
  display: none;
}
</style>
